package org.csu.jpetstoremanagement.controller;

import com.alibaba.fastjson.JSON;
import org.csu.jpetstoremanagement.domain.Category;
import org.csu.jpetstoremanagement.domain.Item;
import org.csu.jpetstoremanagement.domain.Product;
import org.csu.jpetstoremanagement.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("/main")
    public String index() {
        return "/account/sign_in";
    }

    @GetMapping("/categories")
    public String getCategoryList(Model model) {
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "/catalog/categories";
    }

    @GetMapping("/categories/{id}")
    @ResponseBody
    public String getCategory(@PathVariable("id") String categoryId) {
        Category category = catalogService.getCategory(categoryId);
        return JSON.toJSONString(category);
    }

    @PostMapping("/categories")
    public String addCategory(Category category, Model model) {
        System.out.println(category.getCategoryId());
        catalogService.addCategory(category);
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "/catalog/categories";
    }

    @PutMapping("/categories/{id}")
    public String updateCategory(@PathVariable("id") String categoryId, Category category, Model model) {
        if (category.getCategoryId().equals(categoryId)) {
            System.out.println("put: "+categoryId);
            catalogService.updateCategory(category);
            List<Category> categories = catalogService.getCategoryList();
            model.addAttribute("categories", categories);
            return "redirect:/categories";
        } else {
            model.addAttribute("msg", "Request does not match actual data");
            return "/common/error";
        }
    }

    @GetMapping("/deleteCategory")
    public String deleteCategory(String categoryId, Model model) {
        System.out.println(categoryId);
        catalogService.deleteCategory(categoryId);
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "redirect:/categories";
    }

    @GetMapping("/products")
    public String getProducts(Model model) {
        List<Product> products = catalogService.getProductList();
        model.addAttribute("products", products);
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "/catalog/products";
    }

    @GetMapping("/products/{id}")
    @ResponseBody
    public String getProduct(@PathVariable("id") String productId) {
        Product product = catalogService.getProduct(productId);
        return JSON.toJSONString(product);
    }

    @GetMapping("/categories/{id}/products")
    public String getProductsByCategoryId(@PathVariable("id") String categoryId, Model model) {
        List<Product> products = catalogService.getProductListByCategory(categoryId);
        model.addAttribute("products", products);
        return null;
    }

    @PostMapping("/products")
    public String addProduct(Product product, Model model) {
        catalogService.addProduct(product);
        List<Product> products = catalogService.getProductList();
        model.addAttribute("products", products);
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "/catalog/products";
    }

    @PutMapping("/products/{id}")
    public String updateProduct(@PathVariable("id") String productId, Product product, Model model) {
        if (product.getProductId().equals(productId)) {
            catalogService.updateProduct(product);
            model.addAttribute("product", product);
            return "redirect:/products";
        } else {
            model.addAttribute("msg", "Request does not match actual data");
            return "/common/error";
        }
    }

    @GetMapping("/deleteProducts")
    public String deleteProduct(String productId, Model model) {
        catalogService.deleteProduct(productId);
        List<Product> products = catalogService.getProductList();
        model.addAttribute("products", products);
        return "redirect:/products";
    }

    @GetMapping("/items")
    public String getItems(Model model) {
        List<Item> items = catalogService.getItemList();
        model.addAttribute("items", items);
        List<Product> products = catalogService.getProductList();
        model.addAttribute("products", products);
        return "/catalog/items";
    }

    @GetMapping("/items/{id}")
    @ResponseBody
    public String getItem(@PathVariable("id") String itemId) {
        Item item = catalogService.getItem(itemId);
        return JSON.toJSONString(item);
    }

    @GetMapping("/products/{id}/items")
    public String getItemsByProductId(@PathVariable("id") String productId, Model model) {
        List<Item> items = catalogService.getItemListByProduct(productId);
        model.addAttribute("items", items);
        return null;
    }

    @PostMapping("/items")
    public String addItem(Item item, Model model) {
        catalogService.addItem(item);
        List<Item> items = catalogService.getItemList();
        model.addAttribute("items", items);
        List<Product> products = catalogService.getProductList();
        model.addAttribute("products", products);
        return "/catalog/items";
    }

    @PutMapping("/items/{id}")
    public String updateItem(@PathVariable("id") String itemId, Item item, Model model) {
        if (item.getItemId().equals(itemId)) {
            catalogService.updateItem(item);
            model.addAttribute("item", item);
            return "redirect:/items";
        } else {
            model.addAttribute("msg", "Request does not match actual data");
            return "/common/error";
        }
    }

    @GetMapping("/deleteItem")
    public String deleteItem(String itemId, Model model) {
        catalogService.deleteItem(itemId);
        List<Item> items = catalogService.getItemList();
        model.addAttribute("items", items);
        return "redirect:/items";
    }

    @GetMapping("/categories/search")
    public String searchCategory(String categoryId, String name, String description) {
        return null;
    }

    @GetMapping("/products/search")
    public String searchProduct(String categoryId, String name, String description) {
        return null;
    }

    @GetMapping("/items/search")
    public String searchItem(String categoryId, String name, String description) {
        return null;
    }

}
